package com.yoooya.base.admin.controller;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @创建人 Sun Yijing
 * @创建时间 2018-06-28
 */
@Controller
@RequestMapping("/download")
public class DownloadController {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Value("${excel.root.dir}")
	private String excelRootDir;
	@Value("${excel.allow.format}")
	private String excelAllowedFormat;
	@Value("${excel.max.size}")
	private Long excelMaxSize;

	@RequestMapping(value = "/temp/{platform}")
	public ResponseEntity<byte[]> downloadTemp(@PathVariable String platform,
			HttpServletRequest request) throws IOException {
		String path = request.getSession().getServletContext().getRealPath("/");
		String newFileName = "";
		if (platform.equals("customer")) {
			newFileName = "潜在客户信息模板" + ".xlsx";
		} else if (platform.equals("user")) {
			newFileName = "员工档案" + ".xls";
		}
		String fileName = platform + ".xlsx";
		File file = FileUtils.getFile(path, "temp", fileName);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", new String(
				newFileName.getBytes("gbk"), "iso-8859-1"));
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
				headers, HttpStatus.OK);
	}


	/**
	 * 会计客户导入模板
	 * cjianquan 2018/11/7
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/account/accountCustomer")
	public ResponseEntity<byte[]> downloadTempForAccountCustomer(HttpServletRequest request) throws IOException {
		String path = request.getSession().getServletContext().getRealPath("/");
		String newFileName = "会计客户导入模板.xlsx";

		String fileName = "accountCustomer.xlsx";
		File file = FileUtils.getFile(path, "temp", fileName);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", new String(
				newFileName.getBytes("gbk"), "iso-8859-1"));
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
				headers, HttpStatus.OK);
	}
	/**
	 *人事档案导入模板
	 * wuhj 2018/11/9
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/userinfo/personnelRecord")
	public ResponseEntity<byte[]> downloadTempForUser(HttpServletRequest request) throws IOException {
		String path = request.getSession().getServletContext().getRealPath("/");
		String newFileName = "人事档案导入模板.xlsx";

		String fileName = "personalRecords.xlsx";
		File file = FileUtils.getFile(path, "temp", fileName);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("attachment", new String(
				newFileName.getBytes("gbk"), "iso-8859-1"));
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
				headers, HttpStatus.OK);
	}

	@RequestMapping(value = "/downloadImg")
	public ResponseEntity<byte[]> downloadImg(HttpServletRequest request) throws IOException {
		String imgSrc = request.getParameter("imgSrc");
		String imgName = request.getParameter("imgName");
		URL url = new URL(imgSrc);

		URLConnection con = url.openConnection();
		// 输入流
		InputStream is = con.getInputStream();

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);
		headers.setContentDispositionFormData("attachment", new String(imgName.getBytes("gbk"), "iso-8859-1"));

		return new ResponseEntity<byte[]>(IOUtils.toByteArray(is),headers, HttpStatus.OK);
	}



}
